import { Login, LogOut } from '../fn/Login'
import { ReadFile } from '../../src/renderer/utils/tool'

jest.mock('../../src/renderer/utils/tool', () => ({
  ReadFile: jest.fn(),
}));

describe('Login function test', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  test('should return true if username and password match', async () => {
    const form = {
      username: 'test',
      password: 'password',
    };
    const jsonData = {
      account: 'test',
      password: 'password',
    };
    ReadFile.mockReturnValue(jsonData);

    const result = await Login({ commit: jest.fn(), rootState: {} }, form);

    expect(result).toBe(true);
    expect(ReadFile).toHaveBeenCalled();
  });

  test('should return false if username and password do not match', async () => {
    const form = {
      username: 'test',
      password: 'password',
    };
    const jsonData = {
      account: 'test1',
      password: 'password1',
    };
    ReadFile.mockReturnValue(jsonData);

    const result = await Login({ commit: jest.fn(), rootState: {}}, form);

    expect(result).toBe(false);
    expect(ReadFile).toHaveBeenCalled();
  });
});

describe('LogOut function test', () => {
  test('should clear token and userinfo', () => {
    const commitMock = jest.fn();
    const stateMock = {};

    LogOut({ commit: commitMock, state: stateMock });

    expect(commitMock).toHaveBeenCalledWith('SET_TOKEN', '');
    expect(commitMock).toHaveBeenCalledWith('SET_USERINFO', { name: '' });
  });
});